//引入Vue
import Vue from 'vue'
//引用vuex
import vuex from 'vuex'
//使用vuex
Vue.use(vuex)
//引入axios
import axios from "axios"


//vuex模块化和namespaced的使用
//Count相关配置
const countOptions = {
    namespaced:true,
    actions:{
        jiaOdd(context,value){
            // console.log('actions被调用了',context,value);
            if(context.state.sum %2 !==0){
                context.commit('JIA',value)
            }
        },
        jiaWait(context,value){
            setTimeout(()=>{
                context.commit("JIA",value)
            },500)
        }
    },
    mutations:{
        JIA(state,value){
            // console.log('mutations被调用了',state,value);
            state.sum += value;
        },
        JIAN(state,value){
            state.sum -= value;
        },
    },
    state:{
        sum:0,
        school:'TJCU',
        address:'天津',
    },
    getters:{
        bigSum(state){
            return state.sum * 10;
        }
    }
}

//Person相关配置

const personOptions= {
    namespaced:true,
    actions:{
        addWang(context,value){
            if(value.name.indexOf('王') == 0){
                context.commit('ADD_PERSON',value)
            }else{
                alert('请添加姓为"王"的人！！')
            }
        },
        getWord(context){
            axios.get('https://api.uixsj.cn/hitokoto/get?type=social').then(
                response =>{
                    context.commit('GET_WORD',response.data)
                },
                error =>{
                    console.log(error.message)
                }
            )
        }
    },
    mutations:{
        ADD_PERSON(state,value){
            state.personList.unshift(value)
        },
        GET_WORD(state,value){
            state.word = value
        }
    },
    state:{
        word:'',
        personList:[
            {id:1,name:'苏凉'}
        ]
    },
    getters:{}
}


export default new vuex.Store({
    modules:{
        countAbout:countOptions,
        personAbout:personOptions
    }
})